﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Canwest.Broadcasting.Windows.Classes.CW_Common_2008;
using System.Net;
using System.Security.Principal;


namespace StreamingClientSample
{
    public partial class TestForm : Form
    {
        protected DfsConsumer m_dfsObj = null;
        protected String m_username = null;
        protected String m_password = null;
        protected String m_selectedRepository = null;

        public TestForm()
        {
            InitializeComponent();
        }

        private void LoadTestForm(Object sender, EventArgs args)
        {
            //m_username = Framework.GetUserLoginID();
            //m_username = "damadmin";

            if (!String.IsNullOrEmpty(m_username))
            {
                m_username = m_username.Substring(m_username.IndexOf('\\') + 1);
            }
            else
            {
                m_username = "adi_app_service";
            }

            m_UsernameBox.Text = m_username;

            m_dfsObj = new DfsConsumer("DMS", "adi_app_service", "3MCBl@ckB0x");
            m_RepositoryList.Items.AddRange(m_dfsObj.Repositories);

        }

        private void m_LoginButton_Click(object sender, EventArgs e)
        {
            m_password = m_PasswordBox.Text;
            m_username = m_UsernameBox.Text;

            if (String.IsNullOrEmpty(m_username))
            {
                MessageBox.Show("Please enter an username to continue.", "Streaming Client Test");
                return;
            }

            if (String.IsNullOrEmpty(m_password))
            {
                MessageBox.Show("Please enter the password to continue.", "Streaming Client Test");
                return;
            }

            int index = m_RepositoryList.SelectedIndex;
            m_selectedRepository = m_RepositoryList.Items[index].ToString();


            m_dfsObj = new DfsConsumer(m_selectedRepository, m_username, m_password);

            if (null != m_dfsObj)
            {
                MessageBox.Show("Login successful!", m_selectedRepository);
            }
            else
            {
                MessageBox.Show("Login unsuccessful.  Please check your credentials.", m_selectedRepository);
            }

        }

        private void m_StreamButton_Click(object sender, EventArgs e)
        {
            if (null == m_dfsObj)
            {
                return;
            }

            DateTime startTime = DateTime.Now;

            m_dfsObj.setUrls(enAppMode.enDevelopment);

            String mmsUrl = m_dfsObj.getStreamingUrl(m_TapeNumber.Text);

            if (String.IsNullOrEmpty(mmsUrl))
            {
                MessageBox.Show(m_dfsObj.ErrorMessage, "Error in getting Media Services URL.");
                return;
            }

            TimeSpan opTime = DateTime.Now.Subtract(startTime);

            m_TimeTakenLabel.Text = "It took " + opTime.TotalSeconds + "s to retrieve the URL.";

            m_MMS_URL_Label.Text = mmsUrl;

            m_WMP.URL = mmsUrl;

        }

        private void m_CallDocumentumDataAccessButton_Click(object sender, EventArgs e)
        {
            DateTime startTime = DateTime.Now;

            String mmsUrl = null;
            DataSet queryResults = null;
            String strDql = "select r_object_id, mfile_url('wmv', -1, '') from vd_episode_version where v_archive_number = '" + m_TapeNumber.Text + "'";

            using (DocumentumDataAccess.DocumentumDataAccess proxy = new StreamingClientSample.DocumentumDataAccess.DocumentumDataAccess())
            {

                proxy.Credentials = CredentialCache.DefaultNetworkCredentials;

                try
                {
                    queryResults = proxy.QuerySession_fromDocbase(strDql, m_selectedRepository);
                }
                catch (Exception ex)
                {
                    ErrorForm errForm = new ErrorForm(ex, null);
                    errForm.Show();
                }

            }

            if (null == queryResults || null == queryResults.Tables)
            {
                return;
            }

            DataTable dt = queryResults.Tables[0];

            foreach (DataRow dr in dt.Rows)
            {
                if (!String.IsNullOrEmpty(dr[1].ToString()))
                {
                    mmsUrl = dr[1].ToString();
                }
            }

            //if (String.IsNullOrEmpty(mmsUrl))
            //{
            //    return;
            //}

            TimeSpan opTime = DateTime.Now.Subtract(startTime);
            m_DDA_Performance_Label.Text = "It took " + opTime.TotalSeconds + "s to retrieve the URL.";
            m_DDA_URL_Label.Text = mmsUrl;           


        }

    }
}
